<%--
功能：获取指定目录下或者指定商品(可多个)的推荐商品 
--%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@ tag import="java.util.List" %>
<%@ tag import="java.util.ArrayList" %>
<%@ tag import="org.springframework.context.ApplicationContext" %>
<%@ tag import="org.springframework.web.context.support.WebApplicationContextUtils" %>
<%@ tag import="org.apache.commons.lang.ArrayUtils" %>
<%@ tag import="org.apache.commons.lang.StringUtils" %>
<%@ tag import="com.probiz.estore.product.service.ProductManager" %>
<%@ tag import="com.probiz.estore.sales.service.EvalRecommendationManager" %>
<%@ tag import="com.probiz.estore.sales.service.RecommendedTypeManager" %>
<%@ tag import="com.probiz.estore.common.model.product.Product" %>
<%@ tag import="com.probiz.estore.common.model.sales.RecommendedType" %>

<%@ attribute name="recommendedTypeName" type="java.lang.String" required="true" description="推荐类型的名称，如hot_sell,new_arrival等等" %>
<%@ attribute name="sourceKind" type="java.lang.Integer" required="true" description="源类型，0是商品，1是目录" %>
<%@ attribute name="sourceIds" type="java.lang.String" required="true" description="源id(商品id或者商品目录id)的字符串拼接，从','分隔，可以为单个，如2  2,3,5   7,2,1,8等等" %>
<%@ attribute name="firstResult" type="java.lang.Integer" required="false" description="第一条记录起始序号，默认为0" %>
<%@ attribute name="maxResults" type="java.lang.Integer" required="false" description="最多获取记录条数" %>
<%@ attribute name="isNeedRand" type="java.lang.Boolean" required="false" description="如果数量不足时候，是否用随机商品补足，默认flase。注意只有在recommendedTypeName=also_buy的时候才使用该属性，其他类型不能使用，因为有目录的限制，而且随机出来客户会感觉困惑" %>
<%@ attribute name="returnProductListName" type="java.lang.String" required="true" description="要返回的推荐商品列表名称" %>
<%@ attribute name="returnRecommendedTypeName" type="java.lang.String" required="true" description="要返回的推荐类型实体名称" %>

<%

	ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(session.getServletContext());
	EvalRecommendationManager evalRecommendationManager = (EvalRecommendationManager) ctx.getBean("evalRecommendationManager");
	RecommendedTypeManager recommendedTypeManager = (RecommendedTypeManager) ctx.getBean("recommendedTypeManager");
	ProductManager productManager = (ProductManager) ctx.getBean("productManager");
	RecommendedType recommendedType = recommendedTypeManager.getRecommendedTypeByName(recommendedTypeName);
	if(isNeedRand==null)isNeedRand=false;//默认没有随机商品补足
	if(null==firstResult)firstResult = 0;
	if(null==maxResults)maxResults = -1;
	if(StringUtils.isNotBlank(sourceIds)){
		List<Integer> sourceIdList = new ArrayList<Integer>();
		for(String id:sourceIds.split(",")){
			sourceIdList.add(Integer.parseInt(id));
		}
		try{
			List<Product> productList = evalRecommendationManager.getProductsByRecommendedTypeNameBySourceIdList(recommendedTypeName,sourceKind.intValue(),sourceIdList,firstResult.intValue(),maxResults.intValue());
			
			if((productList.size() < maxResults) && isNeedRand && recommendedTypeName.equals("also_buy")){
				//如果数量不足，且需要用随机商品补足
				List<Product> randList = productManager.getRandomProductList(maxResults);
				productList = evalRecommendationManager.mergeList(productList,randList,maxResults);
				
			}
				
			request.setAttribute(returnProductListName,productList);
		}catch(Exception e){
			e.printStackTrace();
		}
	}else{
		if(isNeedRand && recommendedTypeName.equals("also_buy")){
			//如果数量不足，且需要用随机商品补足
			List<Product> randList = productManager.getRandomProductList(maxResults);
			request.setAttribute(returnProductListName,randList);
			
		}
	}	
	request.setAttribute(returnRecommendedTypeName,recommendedType);
%>
<jsp:doBody />
